//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    imgUrls: [
      app.globalData.serverUrl + "uploads/wxxcx/banner.jpg"
    ],
    first: app.globalData.serverUrl +"uploads/wxxcx/11.png",
    second: app.globalData.serverUrl + "uploads/wxxcx/12.png",
    third: app.globalData.serverUrl + "uploads/wxxcx/13.png",
    forth: app.globalData.serverUrl + "uploads/wxxcx/14.png",
    fifth: app.globalData.serverUrl + "uploads/wxxcx/15.png",
    sixth: app.globalData.serverUrl + "uploads/wxxcx/16.png",
    seventh: app.globalData.serverUrl + "uploads/wxxcx/17.png",
    eighth: app.globalData.serverUrl + "uploads/wxxcx/18.png",
    current: 0,  //默认值为0
    indicatorDots: false,
    vertical: true,
    autoplay: true,
    circular: true,
    interval: 6000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0,
    topNavH: '65px',
    topimgH: '200px',
    swiperW: '338px',
    swiperH: '150px',
    swiperT: '189px',
    marginTop: '80px',
    posts:[],
    banners:[],
    setting:null,
  },
  
  onReady: function (e) {
    
  },
  onLaunch: function(){
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启小程序？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })

    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
      wx.showModal({
        title: '提示',
        content: '检查到有新版本，但下载失败，请稍后尝试',
        showCancel: false,
      })
      
    })
  },
  onLoad: function(){
    var that = this
    wx.getSystemInfo({
      success: res => {
        var statusBarHeight = res.statusBarHeight;
        var topNavH = (statusBarHeight + 45);
        that.setData({
          topNavH: topNavH + 'px'
        })
      }
    })
    wx.request({
      url: app.globalData.apiUrl + 'api/scansence/getSetting',
      method: 'GET',
      data: {
        
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if(res.data){
          that.setData({
            banners: res.data.banner,
            setting:res.data.setting
          })
          console.log('setting',res.data.setting);
        }
        
        
      },
      fail: function () {
        
      },
      complete: function () {
      }
    })
    this.getPost();
  },
  getPost(){
    const that = this;
    wx.request({
      url: app.globalData.apiUrl + 'api/scansence/getPosts',
      method: 'GET',
      data: {
        
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if(res.data){
          that.setData({
            posts: res.data
          })
        }
        
        
      },
      fail: function () {
        
      },
      complete: function () {
      }
    })
  },
  onShow:function(){
    if(typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },
  navigatePost: function (e) {
    console.log('e', e)
    let index = e.currentTarget.dataset.key;
    let item = this.data.posts[index];
    
    wx.navigateTo({
      url: '/pages/news/index?src='+encodeURIComponent(item.fullurl),
    })
  },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: '青岛市特种设备安全智慧体验中心',
      path: '/pages/index/index',
      success: function (res) {
        wx.showToast({
          title: '已转发',
          icon: 'success',
          duration: 2000
        })
      }
    }

  },//地图导航
  navgateMap(){
    const that = this;
    console.log('地图导航')
    wx.openLocation({
      name: that.data.setting.location.name,
      latitude:that.data.setting.location.latitude,
      longitude:that.data.setting.location.longitude,
      complete(res){
        console.log(res)
      }
    })
  },
  navgate720(){
    wx.navigateTo({
      url: '/pages/vr/index'
    })
  },
  navgateNotice1(){
    
    wx.navigateTo({
      url: '/pages/news/index?src='+encodeURIComponent(this.data.setting.notice_url),
    })
  },
  scanCode(){
    wx.navigateTo({
      url: '/pages/scancode/index',
    })
  },
  daolan(){
    wx.previewImage({
      urls: [
        this.data.setting.daolan
      ],
    })
  }
})
